//
//  DashanEngine.mm
//  iDashan
//
//  Created by Huahang Liu on 11/2/09.
//  Copyright 2009 University of Southern California. All rights reserved.
//

#import "DashanEngine.h"


@implementation DashanEngine

@synthesize baseAddr;

- (id)init {
    [self initWithBaseAddr:[NSString stringWithFormat:@"http://dashan-net.appspot.com/"]];
    return self;
}
     
- (id)initWithBaseAddr:(NSString*) m_strBaseAddr {
    [super init];
    self.baseAddr = m_strBaseAddr;
    return self;
}

- (void)dealloc {
    [super dealloc];
    [baseAddr release];
}

- (void)callMethod:(NSString*) methodName withParameters:(NSDictionary*) params {

    NSMutableString* urlStr = [NSMutableString stringWithFormat:@"%@/%@", self.baseAddr, methodName];
    bool fisrParam = true;
    for(id key in params) {
        if(fisrParam) {
            [urlStr appendFormat:@"?%@=%@", key, [params objectForKey:key]];
            fisrParam = false;
        }
        else {
            [urlStr appendFormat:@"&%@=%@", key, [params objectForKey:key]];            
        }
    }
    NSURL* theURL = [NSURL URLWithString:urlStr];
    NSMutableURLRequest* theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
    [theRequest setHTTPMethod:@"POST"];    
    NSURLResponse* theResponse = nil;
    NSError* theError = nil;
    NSData* theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
    NSString* theResponseString = [[[NSString alloc] initWithData:theResponseData encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"%@", theResponseString);
    
}

@end
